package com.fs.book.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 第二种方式: 使用注解实现全局异常处理器
 *   异常通知类, 对Controller类的方法增强
 */
@ControllerAdvice
public class BookExceptionHandler {

    //标记这个方法处理那种类型的异常
    @ExceptionHandler(BookBaseException.class)
    @ResponseBody
    public Map handleException(BookBaseException e){
        Map map = new HashMap();
        map.put("status","500");
        map.put("error",e.getMessage());
        return map;
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map handleException(){
        Map map = new HashMap();
        map.put("status","500");
        map.put("error","系统忙,请稍后再试");
        return map;
    }


    @ExceptionHandler(UsernameAndPasswordNotExsitException.class)
    @ResponseBody
    public Map handleException(Exception e){
        Map map = new HashMap();
        map.put("status","510");
        map.put("msg",e.getMessage());
        return map;
    }


}
